package com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAgentDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosAttrDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosOrderDTO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.BcmcPosContext;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> <br>
 * 组装报文代理商节点
 * 
 * @author XXZ<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2020年12月15日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.pos.bcmc.handler.impl <br>
 */
@Service
public class BcmcPosAgentHandler extends AbstractBcmcPosHandler {

    @Override
    public void doHandle(BcmcPosContext context) {
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        KiteSceneInst kiteSceneInst = querySceneInstService.getSceneInst(context.getSceneInstId());
        if (StringUtils.equalsAny(kiteSceneInst.getOperType(), LocalKeyConsts.SCENE_OPER_TYPE_ORDER_SIGN,
            LocalKeyConsts.SCENE_OPER_TYPE_TRANSFER_PLAN_MODIFY)) {
            return;
        }
        Date createDate = kiteSceneInst.getCreateDate();
        String createDateStr = KiteDateUtils.formatDate(createDate, KiteDateUtils.DATE_TIME_FORMAT_14);
        List<KiteAttr> agentAttrs = querySceneInstService.listCommonAttrsByModuleId(context.getSceneInstId(),
            LocalKeyConsts.MODULE_ID_AGENT);
        if (KiteListUtils.isEmpty(agentAttrs)) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("未配置【代理人】属性");
        }
        String agentFlag = querySceneInstService.getExtParameterValue(context.getSceneInstId(), "agentFlag");
        if (!KeyConsts.IFTRUE_T.equals(agentFlag)) {
            return;
        }
        KitePosOrderDTO order = context.getPosDto().getOrder();
        if (Objects.isNull(order)) {
            order = new KitePosOrderDTO();
        }
        List<KitePosAttrDTO> attrs = order.getAttrs();
        if (KiteListUtils.isEmpty(attrs)) {
            attrs = new ArrayList<>();
        }
        KitePosAgentDTO agent = new KitePosAgentDTO();
        agent.setAgentChannelId(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_CHANNELID, "未配置【代理商渠道ID】属性"));
        agent.setAgentChannelName(
            this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_CHANNELNAME, "未配置【代理商渠道名称】属性"));
        agent.setAgentIdType(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_CARDTYPE, "未配置【代理商证件类型】属性"));
        agent.setAgentIdNo(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_CARDNUM, "未配置【代理商证件号码】属性"));
        agent.setAgentAddress(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_CARDADDRESS, "未配置【代理商证件地址】属性"));
        agent.setAgentId(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_ID, "未配置【代理商ID】属性"));
        agent.setAgentName(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_NAME, "未配置【代理商名称】属性"));
        agent.setAgentPhone(this.getAttrValue(agentAttrs, LocalKeyConsts.AGENT_INFO_TEL, "未配置【代理商联系方式】属性"));

        KitePosAttrDTO agentAttr = new KitePosAttrDTO();
        agentAttr.setCode("agent");
        agentAttr.setType("OBJECT");
        agentAttr.setValue(JSONObject.toJSONString(agent));
        agentAttr.setStartDate(createDateStr);
        agentAttr.setEndDate(LocalKeyConsts.ATTR_SEND_DATE);

        attrs.add(agentAttr);
        order.setAttrs(attrs);
        context.getPosDto().setOrder(order);
    }

}
